home *** CD-ROM | disk | FTP | other *** search
/ Software Vault: The Gold Collection / Software Vault - The Gold Collection (American Databankers) (1993).ISO / cdr49 / 302_01.zip / PERSP.C < prev    next >
Text File  |  1993-04-09  |  2KB  |  60 lines

  1. /* Add perspective to transformation matrix
  2.  
  3.    Copyright (c) 1988 by Gus O'Donnell
  4.  
  5.    Revision history:
  6.  
  7.    Version 1.00         February 29, 1988       As released.
  8.  
  9.    Version 1.01         March 20, 1988          Created libraries for all
  10.                                                 memory models
  11.  
  12. */
  13. #include <3d.h>
  14. #include <float.h>
  15. #include <math.h>
  16. #include <stdio.h>
  17.  
  18. void    persp (double s, double d, double f, MATRIX this_mat)
  19.  
  20. /* Add perspective to the transformation matrix.
  21.  
  22. Unlike other transformations, the perspective transformation alters the
  23. value of the scale factor w.  Converting to world coordinates involves
  24. dividing by a factor other than unity.  This has the effect of dividing
  25. the values of the coordinates by the depth, causing objects further away
  26. to appear smaller.
  27.  
  28. The factors required for the perspective transformation are
  29.  
  30.                      s = Viewing screen size
  31.                      d = Viewing screen depth
  32.                      f = Viewing pyramid depth
  33.  
  34. The eye lies at the apex of a pyramid; the object is transformed to
  35. coordinates lying inside this pyramid.  The base of the pyramid is
  36. at depth f, and the viewing screen is defined by the cross section of
  37. the pyramid at depth d.
  38.  
  39. The matrix created for the perspective transformation is
  40.  
  41.                    |  1.0  0.0   0.0             0.0  |
  42.                    |  0.0  1.0   0.0             0.0  |
  43.                    |  0.0  0.0   s/[d(1 - d/f)]  s/d  |
  44.                    |  0.0  0.0  -s/(1 - d/f)     0.0  |
  45.  
  46. The current transformation matrix this_mat is then multiplied by the perspective
  47. transformation matrix, thus concatenating the perspective operation with the
  48. current transformation.
  49. */
  50.  
  51. {
  52.     MATRIX p_mat;
  53.  
  54.     identity (p_mat);
  55.     p_mat [2] [2] = s/(d * (1 - (d/f)));
  56.     p_mat [2] [3] = s/d;
  57.     p_mat [3] [2] = -s/(1 - (d/f));
  58.     p_mat [3] [3] = 0;
  59.     mat_mul (this_mat,p_mat,this_mat);
  60. }